package cn.echo.ecc.web.controller;

import cn.echo.ecc.doc.CourseDoc;
import cn.echo.ecc.dto.CourseEsDto;
import cn.echo.ecc.result.JSONResult;
import cn.echo.ecc.result.PageList;
import cn.echo.ecc.service.ICourseESSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/course")
public class SearchController {

    @Autowired
    private ICourseESSearchService courseESSearchService;

    /**
     * 添加上架的课程
     * @param courseDoc
     * @return
     */
    @PreAuthorize("hasAnyAuthority('es:saveCourse')")
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveCourse(@RequestBody CourseDoc courseDoc){
        courseESSearchService.saveCourse(courseDoc);
        return JSONResult.success();
    }

    /**
     * 删除已上架课程
     * @param id
     * @return
     */
    @RequestMapping(value="/deleteCourse/{id}",method= RequestMethod.DELETE)
    public JSONResult deleteCourse(@PathVariable("id") Long id){
        courseESSearchService.deleteCourse(id);
        return JSONResult.success();
    }

    @RequestMapping(value="/search",method= RequestMethod.POST)
    public JSONResult search(@RequestBody CourseEsDto courseEsDto){
        PageList<CourseDoc> pageList = courseESSearchService.search(courseEsDto);
        return JSONResult.success(pageList);
    }
}
